// 对象
import {checkType} from './check'

/**
 * 遍历对象的键名，返回一个数组
 * eg: const obj = { label: 'arr', name: 'foo' }
 *      objKey(obj) -> ['label', 'name']
 * @param obj 传入对象
 * @return Array
 */
export function objKey(obj:object) {
  let arr = []
  for (let i in obj) {
    arr.push(i)
  }
  return arr
}

/**
 * 遍历对象，返回键值包含数组的直接展开
 * eg:
 * console.log(objValue( { name:'alis', age:'20', arr: [1,2,3,4] } ))
 * -> ['alis', '20', '0', '1', '2', '3']
 * @param obj
 * @return Array
 */
export function objValue(obj:object) {
  let arr = []
  for (let i in obj) {
    // @ts-ignore
    const now = obj[i]
    const objType = checkType(now)
    if(objType.type === 'Array') {
      for (let temp in now) {
        arr.push(temp)
      }
    } else if(objType.type === 'Object') {
      for (let i in now) {
        arr.push(now[i])
      }
    }
  else {
      arr.push(now)
    }
  }
  return arr
}

/**
 * 返回对象的值，不包含数组和内嵌的对象
 * eg:
 * console.log(objValue( { name:'alis', age:'20', arr: [1,2,3,4] },obj:{ name:'foo' }))
 * -> ['alis', '20']
 * @param obj
 * @return Array
 */
export function objVal(obj:object) {
  let arr = []
  for (let i in obj) {
    // @ts-ignore
    const objType = checkType(obj[i])
    // @ts-ignore
    if(objType.type !== 'Array' && objType.type !== 'Object'  ) {
      // @ts-ignore
      arr.push(obj[i])
    }
  }
  return arr
}






